import { reactiveOmit } from '@vueuse/core'
import { reactive } from 'vue'

// 拥有表格的分页逻辑
export default function usePagination(
  props,
  requestHandleSizeChange,
  requestHandleCurrentChange,
) {
  const pagerEmits = reactiveOmit(reactive(props.paginationEmits || {}), [
    'currentChange',
    'sizeChange',
    'current-change',
    'size-change',
  ])

  // 分页器其他属性
  const pagerProps = reactiveOmit(reactive(props.paginationProps || {}), [
    'currentPage',
    'pageSize',
    'total',
  ])

  // 分页大小改变
  const handleSizeChange = (val) => {
    requestHandleSizeChange(val)
    props.paginationEmits?.sizeChange?.(val)
    props.paginationEmits?.['size-change']?.(val)
  }

  // 分页页码改变
  const handleCurrentChange = (val) => {
    requestHandleCurrentChange(val)
    props.paginationEmits?.currentChange?.(val)
    props.paginationEmits?.['current-change']?.(val)
  }

  return {
    handleSizeChange,
    handleCurrentChange,
    pagerEmits,
    pagerProps,
  }
}
